//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#include "Platforms/Android/AHwCamera.h"

#if defined( PLATFORM_ANDROID ) or defined( ANDROID_EMULATOR )

#include "Platforms/Android/APlatform.h"

namespace Engine
{
namespace Platforms
{
	
#	define APLATFORM()			AndroidPlatform::GetInstance()
#	define AJAVAINTERFACE()		APLATFORM()->GetJavaInterface()

/*
=================================================
	constructor
=================================================
*/
	AndroidHwCamera::AndroidHwCamera (const EngineSubSystemsRef ess) :
		BaseObject(ess), _enabled(false)
	{
	}

/*
=================================================
	destructor
=================================================
*/
	AndroidHwCamera::~AndroidHwCamera ()
	{
	}
		
/*
=================================================
	Enable
=================================================
*/
	void AndroidHwCamera::Enable (bool enable)
	{
		ParallelOp	op;
		FunctionBuilder::Create( op.func, AJAVAINTERFACE(), &AndroidJavaInterface::EnableExtension,
								 AndroidPlatform::Extension::CAMERA, enable );

		APLATFORM()->Push( op );
	}
	
/*
=================================================
	Open
=================================================
*/
	void AndroidHwCamera::Open (uint cameraIndex)
	{
		if ( not _enabled ) {
			//WARNING( "Camera not enabled!" );
			Enable( true );
		}
		_SendCommand( ECommand::OPEN, (int)cameraIndex );
	}
	
/*
=================================================
	Close
=================================================
*/
	void AndroidHwCamera::Close (bool saveState)
	{
		_SendCommand( ECommand::CLOSE, (int)saveState );

		_previewCallback		= OnImageCallback_t();
		_pictureCallback		= OnImageCallback_t();
		_faceDetectedCallback	= OnFaceDetected_t();
	}

/*
=================================================
	_SendCommand
=================================================
*/
	void AndroidHwCamera::_SendCommand (ECommand::type cmd, float arg0, float arg1, float arg2)
	{
		ParallelOp	op;
		FunctionBuilder::Create( op.func, AJAVAINTERFACE(), &AndroidJavaInterface::CameraCommand, cmd, arg0, arg1, arg2 );

		APLATFORM()->Push( op );
	}
	
/*
=================================================
	OnPreview
=================================================
*/
	void AndroidHwCamera::OnPreview (const uni_binbuffer_t &data, const ivec2 &size, ImageFormat::type format)
	{
		APLATFORM()->PushToRenderThread( ToRValueRef( *(new _OnImageParallelOp( this, _previewCallback, data, size, format )) ) );
	}
	
/*
=================================================
	OnPreviewFrameAvailable
=================================================
*/
	void AndroidHwCamera::OnPreviewFrameAvailable (void *surfaceTexture)
	{
	}

/*
=================================================
	OnPicture
=================================================
*/
	void AndroidHwCamera::OnPicture (const uni_binbuffer_t &data, const ivec2 &size, ImageFormat::type format)
	{
		APLATFORM()->PushToRenderThread( ToRValueRef( *(new _OnImageParallelOp( this, _pictureCallback, data, size, format )) ) );
	}

/*
=================================================
	OnZoomChanged
=================================================
*/
	void AndroidHwCamera::OnZoomChanged (uint currentZoom, uint maxZoom, float nearFocus, float optimalFocus, float farFocus,
										 float focalLength, float vertViewAngle, float horViewAngle)
	{
	}

/*
=================================================
	OnFaceDetected
=================================================
*/
	void AndroidHwCamera::OnFaceDetected (uint id, const irect &region, const ivec2 &leftEye, const ivec2 &rightEye,
											const ivec2 &mouth, int score)
	{
	}
	
/*
=================================================
	SetStaticParams
=================================================
*/
	void AndroidHwCamera::SetStaticParams (int minExposure, int maxExposure, float exposureStep,
							const format_arr_t &pictureFormats, const ivec2_arr_t &pictureSizes,
							const format_arr_t &previewFormats, const ivec2_arr_t &previewSizes, const ivec2_arr_t &previewFpsRanges,
							const antibanding_arr_t &antibandings, const effect_arr_t &colorEffects, const flashmode_arr_t &flashModes,
							const focusmode_arr_t &focusModes, const scenemode_arr_t &sceneModes, const whitebalance_arr_t &whiteBalances)
	{
		APLATFORM()->PushToRenderThread(
			ToRValueRef( *(new _SetParamsParallelOp(
				DelegateBuilder::Create( AndroidHwCameraPtr(this), &AndroidHwCamera::_SetParams ),
				minExposure, maxExposure, exposureStep,
				pictureFormats, pictureSizes, previewFormats, previewSizes, previewFpsRanges, antibandings, colorEffects,
				flashModes, focusModes, sceneModes, whiteBalances )) )
		);
	}
	
/*
=================================================
	_SetParams
=================================================
*/
	void AndroidHwCamera::_SetParams (_SetParamsParallelOp &op)
	{
		_exposureMinMax = ivec2( op.minExposure, op.maxExposure );
		_previewFpsRanges.SwapMem( op.previewFpsRanges );
		_previewSizes.SwapMem( op.previewSizes );
		_pictureSizes.SwapMem( op.pictureSizes );
		_previewFormats.SwapMem( op.previewFormats );
		_pictureFormats.SwapMem( op.pictureFormats );
		_antibandings.SwapMem( op.antibandings );
		_colorEffects.SwapMem( op.colorEffects );
		_flashModes.SwapMem( op.flashModes );
		_focusModes.SwapMem( op.focusModes );
		_sceneModes.SwapMem( op.sceneModes );
		_whiteBalances.SwapMem( op.whiteBalances );
	}

	
#	undef APLATFORM
#	undef AJAVAINTERFACE

}	// Platforms
}	// Engine

#endif	// PLATFORM_ANDROID or ANDROID_EMULATOR